#include "Generator.h"
#include "Events\CSGD_EventSystem.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "Camera.h"


Generator::Generator(void)
{
	objectID = "Generator";
	IsPowered = true;
	CheckPointIsPowered = false;

	PoweredStateImageID = -1;
	UnpoweredImageID = -1;
	HeatPhysics::SetCoolRate(0.8f);
	HeatPhysics::SetCurTemp(0.0f);
}


Generator::~Generator(void)
{

}

//Overrides
void Generator::Update(float deltaTime)
{
	HeatPhysics::Update(deltaTime);

	//Check if generator is frozen
	if(HeatPhysics::CurTemp() < HeatPhysics::StartTemp() && IsPowered == true)
	{
		SetIsPowered(false);
		//Update spark particles
	}
	
	if( HeatPhysics::CurTemp() >= HeatPhysics::StartTemp() && IsPowered == false )
	{
		SetIsPowered(true);
	}
}
void Generator::Render(void) const
{
	//21 - 3
	//Draw Generator
	CSGD_TextureManager::GetInstance()->Draw(PoweredStateImageID, (int)(positionX - Camera::GetInstance()->OffsetX()),
																	 (int)(positionY - Camera::GetInstance()->OffsetY()));
	//Draw 
	if(IsPowered == true)
	{
		CSGD_TextureManager::GetInstance()->Draw(PoweredStateImageID, (int)(positionX - Camera::GetInstance()->OffsetX()),
																	 (int)(positionY - Camera::GetInstance()->OffsetY()));
	}
	else
	{
		CSGD_TextureManager::GetInstance()->Draw(UnpoweredImageID,(int)(positionX - Camera::GetInstance()->OffsetX()),
																	(int)(positionY - Camera::GetInstance()->OffsetY()));
		//TODO: render spark particles
	}
	D3DCOLOR tempColor = GetColorFromTemp();
	BYTE alpha = tempColor >> 24;
	BYTE red = (tempColor >> 16) & 0xFF;
	BYTE green = (tempColor >> 8) & 0xFF;
	BYTE blue = tempColor & 0xFF;
	alpha -= 90;
	tempColor = D3DCOLOR_ARGB(alpha,red,green,blue);

	
	CSGD_Direct3D::GetInstance()->DrawRect(hitBox,tempColor);
}
void Generator::HandleCollision(GameObject& other, CollisionInfo& info)
{
	//Check input and set active
	//if( other.GetObjectID() == "Player" )
	//{
	//	if(CSGD_DirectInput::GetInstance()->KeyPressed( DIK_SPACE ) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(5))
	//	{
	//		IsPowered = true;
	//	}
	//}
}

//Check Point Overrides
void Generator::SetCheckPointData()
{
	CheckPointIsPowered = IsPowered;
}
void Generator::ResetToCheckPoint()
{
	IsPowered = CheckPointIsPowered;

	//Reset objects being effected
	if( IsPowered == true )
	{
		CSGD_EventSystem::GetInstance()->SendEvent("GeneratorActive", nullptr, nullptr, (void*)this);
	}
	else
	{
		CSGD_EventSystem::GetInstance()->SendEvent("GeneratorNotActive", nullptr, nullptr, (void*)this);
	}
}

void Generator::SetIsPowered( bool Active)
{
	this->IsPowered = Active;

	if( IsPowered == true )
	{
		CSGD_EventSystem::GetInstance()->SendEvent("GeneratorActive", nullptr, nullptr, (void*)this);
	}
	else
	{
		CSGD_EventSystem::GetInstance()->SendEvent("GeneratorNotActive", nullptr, nullptr, (void*)this);
	}

	//Which Image to use
	if( IsPowered == true )
		imageID = PoweredStateImageID;
	else
		imageID = UnpoweredImageID;
}


void Generator::SetPoweredImageID( int index )
{
	this->PoweredStateImageID = index;
}
void Generator::SetUnpoweredImageID( int index )
{
	this->UnpoweredImageID = index;
}